package com.hl.sfzy.service.wxwappService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.wxwapp.Heartmark;
import com.hl.sfzy.mapper.wxwappMapper.HeartmarkMapper;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: HeartMarkService
 * @author: WXW
 * @date: 2024/9/10 10:32
 * @Version: 1.0
 * @description:
 */
@Service
public class WXWHeartMarkService {
    @Autowired
    private HeartmarkMapper heartmarkMapper;

    public PageInfo<Heartmark> findAllHeartMark(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Heartmark> heartmarks = heartmarkMapper.selectAll();
        PageInfo<Heartmark> pageInfo = new PageInfo<>(heartmarks);
        return pageInfo;
    }

    //查询user表的信息
    public R findUserInfo() {
        return R.success(heartmarkMapper.selectAllUserById());
    }
    //新增数据
    public int addHeartMark(Heartmark heartmark) {
        int i = heartmarkMapper.insertSelective(heartmark);
        return i;
    }

    //删除数据
    public R deleteHeartMark(Integer id){
        int i = heartmarkMapper.deleteByPrimaryKey((long) id);
        if (i > 0) {
            return R.success(i);
        }else {
            return R.failure("删除失败");
        }
    }

    //根据id查询数据(修改界面)
    public R findHeartMarkById(Integer id){
        return R.success(heartmarkMapper.selectByPrimaryKey((long)id));
    }

    //修改数据
    public R updateHeartMark(Heartmark heartmark){
        int i = heartmarkMapper.updateByPrimaryKeySelective(heartmark);
        if (i > 0) {
            return R.success("修改成功");
        }
        return R.failure("修改失败");
    }

    //根据测评名模糊查询
    public R findHeartMarkByName(String name,Integer page,Integer size){
        PageHelper.startPage(page,size);
        List<Heartmark> list = heartmarkMapper.selectByName(name);
        PageInfo<Heartmark> pageInfo = new PageInfo<>(list);
        return R.success(pageInfo);
    }

    //app端心理知识测评列表点击跳转详情
    public R findHeartMarkByIdApp(Integer id) {
        return R.success(heartmarkMapper.selectByHeartMarkId(id));
    }
}
